#include "key.h"
#include "LED.h"
#include "delay.h"
#include "freertos.h"
#include "task.h"
#include "freertos_demo.h"

/*
	KEY0 -- PA0
*/

extern TaskHandle_t Led2_Hanlder; 

void KEY_Init()
{
	GPIO_InitTypeDef GPIOInitStructure;
	
	GPIO_StructInit(&GPIOInitStructure);
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	GPIOInitStructure.GPIO_Pin = GPIO_Pin_0;
	GPIOInitStructure.GPIO_Mode = GPIO_Mode_IN;
	GPIOInitStructure.GPIO_OType = GPIO_OType_PP;
	GPIOInitStructure.GPIO_PuPd = GPIO_PuPd_UP;
	GPIOInitStructure.GPIO_Speed = GPIO_Speed_100MHz;

	GPIO_Init(GPIOA, &GPIOInitStructure);
}

u8 Read_key()
{
	uint16_t temp = GPIO_ReadInputData(GPIOA);

	if (temp & GPIO_Pin_0) {	
	//如果PA0为高电平
		return 1;
	} else {
	//如果PA0为低电平
		return 0;
	}
}


void EXTI0_IRQHandler(void)  //外部中断线0中断服务函数
{
//    printf("the key is pressed!\n");
//    
//    LED_Toggle(LED2);
//     
//	EXTI_ClearITPendingBit(EXTI_Line0); //清除LINE0上的中断标志位 
}



void EXTIX_Init(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;// 设置中断优先级结构体变量
	EXTI_InitTypeDef EXTI_InitStructure;//设置外部中断结构体结构体变量
	
	//KEY_Init(); //按键对应的IO口初始化
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG,ENABLE); //使能SYSCFG时钟
	SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); //PA0连接中断线0，映射按键KEY_UP   简单来说 接下来四条语句实现把引脚接在对应的中断线上，类似于复用功能

	//KEY0是低电平0有效，
	//所以上升沿和下降沿需要分开设置
	
	EXTI_InitStructure.EXTI_Line=EXTI_Line0;    //中断线0
	EXTI_InitStructure.EXTI_LineCmd=ENABLE;     //中断使能
	EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;//模式中断
	EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Rising;//上升沿有效 通过原理图可以发现KEY_UP按键左侧接V3.3，右侧接引脚，按键没有按下时，引脚呈现低电平，按键一旦按下，引脚呈现高电平，按键按下也就表示着上升沿有效
	EXTI_Init(&EXTI_InitStructure);//初始化外部中断
	
	
	NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQn;//外部中断0
	NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能外部中断0
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x06;//抢占优先级5
	NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x00;//响应优先级0
	NVIC_Init(&NVIC_InitStructure);//初始化中断优先级	
}
 



